<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" :rows="10" type="textarea"/>
      </el-form-item>

      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">
      </el-form-item>

      <el-form-item>
        <el-button type="primary" saveBtnDisabled @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/vod/teacher';
export default {
  data() {
    return {
      teacher: {
        sort: 0,
        level: 1
      },

    }
  },
  // 页面渲染成功前
  created() {
    
    if(this.$route.params.id){
      
      this.getTeacher(this.$route.params.id)
    }
  },
  methods: {

    // 根据id查询teacher
    getTeacher(id){
      teacherApi.getById(id).then(res=>{
        // debugger;
        this.teacher = res.data
      })
    },

    // 保存
    saveOrUpdate() {
      teacherApi.save(this.teacher).then(res=>{
        // console.log(res.code)

        if(res.code === 20000){
          this.$message.success(res.message);
           this.$router.push({ path: '/vod/teacher/list' })
        }else {
          this.$message.error(res.message);
        }

      }).catch(err=>{
        console.log(err.code)
        this.$message.error("系统错误【400,404】");
      })
    },
  },
  // 页面加载时
  mounted() {

  }
}
</script>

<style scoped>
  .avatar-uploader .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar-uploader .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader img {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
